package com.lfs.market.service.impl;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.lfs.market.exception.ServiceException;
import com.lfs.market.mapper.ProductTypeMapper;
import com.lfs.market.model.dto.SystemProductTypePageDto;
import com.lfs.market.model.entity.ProductType;
import com.lfs.market.model.enums.ResultCode;
import com.lfs.market.service.ProductTypeService;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.Date;

@Service
public class ProductTypeServiceImpl extends ServiceImpl<ProductTypeMapper, ProductType> implements ProductTypeService {

    @Resource
    private ProductTypeService productTypeService;

    /**
     * 查询商品分类列表
     *
     * @param dto
     * @return
     */
    @Override
    public Page getTypePageList(SystemProductTypePageDto dto) {
        Page<ProductType> page = lambdaQuery()
                .like(StrUtil.isNotEmpty(dto.getKey()), ProductType::getTypeCode, dto.getKey()).or()
                .like(StrUtil.isNotEmpty(dto.getKey()), ProductType::getTypeName, dto.getKey())
                .orderByDesc(ProductType::getCreateTime)
                .page(new Page<>(dto.getPageNumber(), dto.getPageSize()));
        return page;
    }

    /**
     * 添加商品分类
     *
     * @param type 分类
     * @return
     */
    @Override
    public void createType(ProductType type) {
        if (StrUtil.isEmpty(type.getTypeCode()) || StrUtil.isEmpty(type.getTypeName()))
            throw new ServiceException(ResultCode.ValidateError, "分类名或分类代码为空");
        ProductType one = productTypeService.lambdaQuery()
                .eq(ProductType::getTypeName, type.getTypeName())
                .or()
                .eq(ProductType::getTypeCode, type.getTypeCode())
                .one();
        if (!BeanUtil.isEmpty(one)) throw new ServiceException(ResultCode.Fail, "分类名或代码重复");
        ProductType productType = new ProductType();
        productType.setTypeCode(type.getTypeCode());
        productType.setTypeName(type.getTypeName());
        productType.setCreateTime(new Date());
        productType.setUpdateTime(new Date());
        boolean save = productTypeService.save(productType);
        if (!save) throw new ServiceException(ResultCode.SaveError);
    }

    /**
     * 编辑商品分类
     *
     * @param type
     * @return
     */
    @Override
    public void editType(ProductType type) {
        if (StrUtil.isEmpty(type.getTypeName()))
            throw new ServiceException(ResultCode.ValidateError);
        ProductType productType = productTypeService.getById(type.getId());
        if (BeanUtil.isEmpty(productType)) throw new ServiceException(ResultCode.NotFindError);
        productType.setTypeName(type.getTypeName());
        productType.setUpdateTime(new Date());
        boolean update = productTypeService.updateById(productType);
        if (!update) throw new ServiceException(ResultCode.UpdateError);
    }
}
